package com.weetop.aklyscan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.weetop.aklyscan.R;
import com.weetop.aklyscan.base.BaseActivity;
import com.weetop.aklyscan.base.mvp.BaseModel;
import com.weetop.aklyscan.bean.BaseList;
import com.weetop.aklyscan.bean.RecordListBean;
import com.weetop.aklyscan.presenter.InventoryPresent;
import com.weetop.aklyscan.view.InventoryView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class InventoryRecordActivity1 extends BaseActivity<InventoryPresent> implements InventoryView {

    public static final int TYPE_FACTORY_PEIJIAN = 1001;
    public static final int TYPE_FACTORY_CHANPIN = 1002;

    public static final int TYPE_FACTORY_PEIJIAN_PRODUCT = 1003;

    private int type = TYPE_FACTORY_CHANPIN;
    String order_sn;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private CommonRecyclerAdapter<RecordListBean> adapter;
    private List<RecordListBean> list = new ArrayList<>();

    @Override
    protected InventoryPresent createPresenter() {
        return new InventoryPresent(this, this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inventory_record1);
        ButterKnife.bind(this);
        order_sn = getIntent().getStringExtra("order_sn");
        int order_type = getIntent().getIntExtra("type", 0);
        if(order_type == 0){
            type = TYPE_FACTORY_CHANPIN;
        }else if(order_type == 1){
            type = TYPE_FACTORY_PEIJIAN_PRODUCT;
        }else{
            type = TYPE_FACTORY_PEIJIAN;;
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new CommonRecyclerAdapter<RecordListBean>(mActivity, R.layout.item_inventory_record_list, list) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, RecordListBean item, int position) {
                helper.setText(R.id.tv_xuhao, position + 1 + "");
                if (type == TYPE_FACTORY_PEIJIAN || type == TYPE_FACTORY_PEIJIAN_PRODUCT) {
                    helper.setText(R.id.tv_time, item.getYp_time());
                    helper.setText(R.id.tv_num, item.getCount_num());
                } else {
                    helper.setText(R.id.tv_num, item.getTotal_num());
                    helper.setText(R.id.tv_time, item.getPd_time());
                }
                helper.setText(R.id.tv_user, item.getApply_user_name());
                helper.setOnClickListener(R.id.ll, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(type == TYPE_FACTORY_PEIJIAN_PRODUCT){
                            startActivity(new Intent(InventoryRecordActivity1.this, InventoryPeijianRecordDetail1Activity.class)
                                    .putExtra("order_sn", order_sn)
                                    .putExtra("type", type)
                                    .putExtra("time", type == TYPE_FACTORY_PEIJIAN || type == TYPE_FACTORY_PEIJIAN_PRODUCT ? item.getYp_time() : item.getPd_time()));
                        } else if(type == TYPE_FACTORY_PEIJIAN){
                            startActivity(new Intent(InventoryRecordActivity1.this, InventoryPeijianRecordDetailActivity.class)
                                    .putExtra("order_sn", order_sn)
                                    .putExtra("type", type)
                                    .putExtra("time", type == TYPE_FACTORY_PEIJIAN || type == TYPE_FACTORY_PEIJIAN_PRODUCT ? item.getYp_time() : item.getPd_time()));
                        }else {
                            startActivity(new Intent(InventoryRecordActivity1.this, InventoryRecordDetailActivity.class)
                                    .putExtra("order_sn", order_sn)
                                    .putExtra("type", type)
                                    .putExtra("time", type == TYPE_FACTORY_PEIJIAN  || type == TYPE_FACTORY_PEIJIAN_PRODUCT? item.getYp_time() : item.getPd_time()));
                        }

                    }
                });
            }
        };
        recyclerView.setAdapter(adapter);
        if (type == TYPE_FACTORY_CHANPIN) {
            mPresenter.Into50(order_sn);
        } else if(type == TYPE_FACTORY_PEIJIAN_PRODUCT){
            mPresenter.Into67(order_sn);
        }else {
            mPresenter.Into73(order_sn);
        }
    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    public void getInto50Success(BaseModel<List<RecordListBean>> o) {
        List<RecordListBean> listBeans = o.getData();
        list.clear();
        adapter.clear();
        for (int i = 0; i < listBeans.size(); i++) {
            if (!TextUtils.isEmpty(listBeans.get(i).getPd_time())) {
                list.add(listBeans.get(i));
            }
        }
        adapter.addAll(list);
    }

    @Override
    public void getInto73Success(BaseModel<BaseList<RecordListBean>> o) {
        List<RecordListBean> listBeans = o.getData().getList_data();
        list.clear();
        adapter.clear();
        for (int i = 0; i < listBeans.size(); i++) {
            if (!TextUtils.isEmpty(listBeans.get(i).getYp_time())) {
                list.add(listBeans.get(i));
            }
        }
        adapter.addAll(list);
    }
}
